﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using ServerBase.LunarSocket;
using ServerBase.Protocol;
using UtilLib;
using static ServerBase.Config.Conf;
using static ServerBase.Service.BaseDispatch;
using static UtilPublic.Redis.ServerRedis;
using ServerGame.Service;
using System.Collections.Generic;
using UtilPublic.ServerLoger;
using ServerBase.Service;
using ServerBase.Config;
using static ServerGame.Service.Dispatcher;
using ServerBase.Client;
using System;
using ServerPublic.Data;
using ServerGame.GameObject;
using static UtilPublic.Serialization.Jsh;
using ServerPublic.DataBaseMysql;
using UtilPublic.ApiDispatcher;
using ServerGame.Data;
using System.Text;

namespace ServerGame.Manager
{
    /// <summary>
    /// 管理 角色
    /// </summary>
    public static partial class CharMgr
    {
        public static void LoopPlayerMinite1(Player player)
        {
            UpdateAge(player);
        }
        //计算 年龄
        public static void UpdateAge(Player player)
        {
            var DtNow = DateTime.Now;

            //现实时间 10天 = 1岁
            var Age = 14 + (DtNow - new DateTime(player.DaMisc.TicksRegister)).TotalDays / 10;
            var newAge = (int)Age;
            if (player.Age != newAge)
            {
                Hp(player);
            }
        }
        //计算 状态
        public static void Hp(this Player player)
        {
            //todo 用内功法术  计算内力和法力  用根骨灵性内力和法力 计算气血精神
            player.UsageGet(EUsageType.基本内功, out _, out var levelforce);
            player.UsageGet(EUsageType.基本法术, out _, out var levelspell);
            var mxforce = 10 * levelforce;
            var mxmana = 10 * levelspell;
            mxforce += player[EProp.adforce];
            mxmana += player[EProp.admana];
            mxforce = Math.Max(mxforce, 10);
            mxmana = Math.Max(mxmana, 10);
            player[EProp.mxforce] = mxforce;
            player[EProp.mxmana] = mxmana;

            //年龄和天赋计算出的气血和精神
            var age = player.Age;
            var kee = (player[EProp.str] + player[EProp.con]) / 2;
            var sen = (player[EProp.itn] + player[EProp.spi]) / 2;
            (int mxkee, int mxsen) = true switch
            {
                _ when age < 14 => (10 * kee, 10 * sen),
                _ when age < 30 => ((age - 4) * kee, (age - 4) * sen),
                _ => (26 * kee, 26 * sen)
            };
            if (mxforce > 0)
            {
                mxkee += mxforce / 4;
            }
            if (mxmana > 0)
            {
                mxsen += mxmana / 4;
            }
            mxkee += player[EProp.adkee];
            mxsen += player[EProp.adsen];
            player[EProp.mxkee] = mxkee;
            player[EProp.mxsen] = mxsen;

            //食物饮水
            var weight = 40_000 + (player[EProp.str] - 10) * 2000;
            var mxfood = Math.Max(weight / 200, 200);
            var mxwater = Math.Max(weight / 200, 200);
            mxfood += player[EProp.adfood];
            mxwater += player[EProp.adwater];
            player[EProp.mxfood] = mxfood;
            player[EProp.mxwater] = mxwater;

            player.CheckMax();
        }
        //满状态
        public static void Full(this MudChar obj)
        {
            obj[EProp.food] = obj[EProp.mxfood];
            obj[EProp.water] = obj[EProp.mxwater];

            obj[EProp.efkee] = obj[EProp.mxkee];
            obj[EProp.efsen] = obj[EProp.mxsen];

            obj[EProp.kee] = obj[EProp.mxkee];
            obj[EProp.sen] = obj[EProp.mxsen];

            obj[EProp.force] = obj[EProp.mxforce] * 2;
            obj[EProp.mana] = obj[EProp.mxmana] * 2;
        }
        // 获取score
        public static void GetInfo(this Player player, CLS_InfoScore info)
        {
            info.Puid = player.Puid;

            info.Gem = player.DaBasic.Gem;
            info.Gold = player.DaBasic.Gold;
            info.Pot = player.DaBasic.Pot;

            info.VipLevel = player.DaBasic.VipLevel;
            info.VipExp = player.DaBasic.VipExp;


            info.Name = player.Name;
            info.Gender = player.Gender;
            info.Family = player.Family;
            info.Title = player.Title;
            info.Age = player.Age;

            info.Level = player.DaCommon.Level;
            info.Exp = player.DaCommon.Exp;

            for (int prop = (int)EProp.str; prop <= (int)EProp.cspell; prop++)
            {
                info.DictProp[prop] = player[(EProp)prop];
            }
            for (int prop = (int)EPropCbt.damage; prop <= (int)EPropCbt.spell; prop++)
            {
                info.DictPropCbt[prop] = player[(EPropCbt)prop];
            }
        }
        //挂机信息
        public static void GetInfo(this Player player, CLS_InfoOffline info)
        {
            info.OfflineMode = player.DaBasic.OfflineMode;
            if (player.DaBasic.OfflineMode != 0)
            {
                info.OfflineTime = new DateTime(player.DaBasic.OfflineTicks).ToUnixTimeMilliseconds();
                info.OfflineTs = (DateTime.Now - new DateTime(player.DaBasic.OfflineTicks)).ToString("d'天'h'小时'm'分's'秒'");
            }
        }
        public static void RoomInfoPlayer(this Player player, out CLS_Room_InfoPlayer info)
        {
            info = new CLS_Room_InfoPlayer();
            info.Puid = player.Puid;
            info.Name = player.Name;
            info.Desc = player.Desc;
        }
    }
}
